﻿using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using WpfApp1.Models;

namespace WpfApp1.Controls
{
    public class DragThumb : Thumb
    {
        public DragThumb()
        {
            DragDelta += DragThumb_DragDelta;
            DragStarted += DragThumb_DragStarted;
            DragCompleted += DragThumb_DragCompleted;
        }

        private SharpBase? SharpBase => this.DataContext as SharpBase;


        private void DragThumb_DragStarted(object sender, DragStartedEventArgs e)
        {
            GetDesignerCanvas(this)?.Focus();
        }

        private void DragThumb_DragCompleted(object sender, DragCompletedEventArgs e)
        {

        }

        private void DragThumb_DragDelta(object sender, DragDeltaEventArgs e)
        {
            Debug.WriteLine($"DragThumb_DragDelta--{DateTime.Now.ToString("yyyyMMdd-HHmmssfff")}");
            if (SharpBase != null)
            {
                //此处可以移动控件位置
                SharpBase.Left += e.HorizontalChange;
                SharpBase.Top += e.VerticalChange;
            }
        }

        private Canvas? GetDesignerCanvas(DependencyObject element)
        {
            while (element != null && !(element is Canvas))
            {
                element = VisualTreeHelper.GetParent(element);
            }

            return element as Canvas;
        }
    }
}
